var webRoot = 'http://app.cqyiyixin.com';
var apiRoot = webRoot + '/api/';
var p1=/^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;//手机号码格式验证
var passReg = /^[A-Za-z0-9]{6,16}$/;//验证密码规范
var card = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;//验证身份证号
var p3 = /^[0-9]*$/; //判断是否为数字
var m = /^(([1-9]\d*)|0)(\.\d*)?$/;	//判断金钱
var a = /^([0-9]|[0-9]{2}|100)$/; //年龄
var h = /^[1-2]\d{2}$/; //身高
var w = /^\d{2,3}$/; //体重
var re= /^[1-9][0-9]{5}$/;	//邮政编码
//获取头像
var getAvatar = function(avatar){
	if(avatar == null || avatar == ''){ 
		return '../images/iconic.png';
	}
	return avatar.indexOf('http')<0? webRoot + avatar : avatar;
}
/**
 * 判断用户是否登录
 * @param {webview} page
 */
var isLogin = function (){
	var uid = plus.storage.getItem('uid');
	if(!uid){
		toast('你还未登录');
		plus.webview.create('me-login.html' , 'me-login.html' , {} , {}).show('slide-in-right');
	}
}
/**
 * 获取验证码
 * @param phone 手机号码
 * @return null
 */
function getverify(user){
	var tmp = Date.parse(new Date()).toString();
	tmp = tmp.substr(0, 10);
	console.log(tmp);
	showWating();
	$.ajax({
		type :'post',
		url :apiRoot+"/home/Interface/tortoise",
		data :{phone:user, tmp:tmp},
		cache :false,
		success : function(data){
			console.log(data);
			plus.nativeUI.closeWaiting();
			if(data == '0'){
				toast('验证码发送成功');
				time_asc();
			}else{
				toast('验证码发送失败');
			}
			return;
		} , 
		error : function(e){
			console.log(JSON.stringify(e));
			toast('失去网络连接');
		}
	})
	//测试js是否被多次访问
	$.ajax({type:"get",url:apiRoot+"/home/Interface/attempt"});
}
/**
 * 倒计时
 */
function time_asc(){
	$('#getcode').attr('disabled' , true);
	$('.user').attr('disabled' , true);
	$('.phone').attr('disabled' , true);
	var code = '';
	var second = 180;
	code = setInterval(function(){
		if(second <= 0){
			clearInterval(code);
			reset_input(code);  
		}else{
			$('#getcode').val('重新获取'+second -- + 's');
		}
	} , 1000);  
}

/**
 * 重设状态
 */
function reset_input(code){
	clearInterval(code);
	$('#getcode').removeAttr('disabled');
	$('#getcode').val('重发');
	$('.user').removeAttr('disabled');
	$('.phone').removeAttr('disabled');
}
/**
 * 防止页面跳转时白频，页面传值
 * @param {String} url:页面
 * @param {Object} zhi:传的值
 */
function goNewPage(url, zhi){
	var ws = plus.webview.getWebviewById(url); 
	if(ws){//避免多次打开同一个页面
		return false;
	}else{
//		console.log(JSON.stringify(zhi))
		if(zhi){
			var newPage = plus.webview.create(url,url,{},zhi);
		}else{
			var newPage = plus.webview.create(url,url);
		}
		newPage.addEventListener('close',function(){//页面关闭后可再次打开
			newPage = null;
		},false);
		showWating();
		newPage.addEventListener('loaded',function(){
			newPage.show('pop-in',50);
			plus.nativeUI.closeWaiting();
		},false)
	}
}
//页面跳转
var goUrl = function(url){
	showWating();
	plus.webview.open(url,url,{},'slide-in-right');
}
//弹窗
var toast = function(info){
	plus.nativeUI.toast(info);
}
//网络连接失败提示
var errortoast = function(e){
	plus.nativeUI.closeWaiting();
	toast('连接超时，请检查网络连接');
}
//下拉刷新
function PullToRefresh(ws, uid, gid){
	ws = plus.webview.currentWebview(); 
	ws.setPullToRefresh({support:true,
		height:"50px",
		range:"200px",
		contentdown:{  
			caption:"下拉可以刷新"
		},
		contentover:{
			caption:"释放立即刷新"
		},
		contentrefresh:{
			caption:"正在刷新..."
		} 
	},function(){
		if(uid && gid){
			$.ajax({
				type:"get",
				url:apiRoot+"?m=home&c=member&a=isStatus",
				data :{
					uid: uid,
					gid:gid
				},
				dataType : 'json',
				success :function(data){
					console.log(JSON.stringify(data));
					if(data.status){
						plus.storage.setItem('status',data.status+'');//用认证状态
						plus.storage.setItem('authtime',data.authtime+'');//认证成功时间
					}
				},
				error :function(e){
					console.log(JSON.stringify(e));
					errortoast();
				}
			});
		}
		setTimeout(function(){
			ws.reload('none');
			ws.endPullToRefresh();
		},1500);
	});
}
//等待窗口
var showWating = function(info , time){
	if(info == null){
		info = '加载中...';
	}
	if(!time || time==0){
		time = 5000;  
	}
	var waiting = plus.nativeUI.showWaiting(info,{width:'80px',height:'80px',background:'rgba(0,0,0,0.3)'});
	setTimeout(function(){
		if(waiting){
			waiting.close();
		}
	},time);
}    
//将发布的多图拆分成数组
function splitArray(str){
	var arr =  str.replace(/\|\-\|/g, '');
	var arr2 = arr.split('|,|');
	var arr3 = [];
	for(var i=0;i<arr2.length;i++){
		if(arr2[i] != ''){
			arr3.push(arr2[i]);
		}
	}
	return arr3;
}
